package com.example.administrator.myview.broadcast;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import com.example.administrator.myview.R;


/**
 * 拦截系统短信 暂时没成功
 * Created by Administrator on 2017/11/23.
 */

public class SmsActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SMSContentObserver smsContentObserver = new SMSContentObserver(this, mHandler);
        //注册要观察的url
        getContentResolver().registerContentObserver(Uri.parse("content://sms/inbox"), true, smsContentObserver);
    }

    SQLiteDatabase database;

    //通过Handler传递数据
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    //在数据库中插入数据的时候，首先应该有一个ContentValues的对象所以
                    ContentValues cv = (ContentValues) msg.obj;
//                      database = new SQLiteDatabase(SmsActivity.this);
//                   dbRead = database.getRawDatabase();
//                    cursorRead = dbRead.query("MyMessage", null,"body=? and date=?",
//                            new String[]{(String) cv.get("body"), (String) cv.get("date")}, null, null, null);
//                    if (cursorRead.getCount() == 0) {
//                        database = new Database(this);      //这里是自己创建的一个数据库，把监听到的短信保存到这数据库
//                        dbWrite = database.getWritableDatabase();        //
//                        dbWrite.insert("MyMessage", null, cv);           //
//                      //  etNumber.setText((String) cv.get("address"));
//                        getContentResolver().delete(Uri.parse("content://sms/inbox"), "body=? and date=?",                         new String[]{(String) cv.get("body"), (String) cv.get("date")});//这里把那条数据给删除了，起到截获短信效果
////                        refreshListView();
//                    }
            }
        }
    };
}
